package io.dshubs.archives.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.dshubs.archives.constant.ArchivesConstant;
import io.dshubs.archives.dao.ArchiveTransferListMapper;
import io.dshubs.archives.entity.ArchiveTransferList;
import io.dshubs.archives.entity.vo.ArchiveTransferListStatusVo;
import io.dshubs.archives.service.ArchiveTransferListService;
import io.dshubs.commons.mybatis.enums.DelFlagEnum;
import io.dshubs.commons.mybatis.service.impl.BaseServiceImpl;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.commons.security.user.UserDetail;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.enums.SuperAdminEnum;
import io.dshubs.commons.tools.enums.SuperTenantEnum;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.dto.SysDeptDTO;
import io.dshubs.dto.SysTenantDTO;
import io.dshubs.feign.SysFeignClient;
import io.dshubs.feign.TenantFeignClient;
import io.dshubs.feign.UserFeignClient;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案移交记录表 服务实现类
 * </p>
 *
 * @author tan
 * @since 2021-08-26
 */
@Service
public class ArchiveTransferListServiceImpl extends BaseServiceImpl<ArchiveTransferListMapper, ArchiveTransferList> implements ArchiveTransferListService {
    @Autowired
    private TenantFeignClient tenantFeignClient;

    @Autowired
    private SysFeignClient sysFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;
    @Override
    public PageData<ArchiveTransferList> page(Map<String, Object> params) {
        IPage<ArchiveTransferList> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        PageData<ArchiveTransferList> pageData = getPageData(page, ArchiveTransferList.class);
        List<ArchiveTransferList> list = pageData.getList();
        if(list.size() > 0) {
            list.forEach(item->{
                Integer batchNo = this.baseDao.selectCount(
                        new QueryWrapper<ArchiveTransferList>().eq("batch_no", item.getBatchNo()));
                item.setTransferCount(batchNo);
                SysDeptDTO data = sysFeignClient.get(item.getTransferDeptId()).getData();
                item.setTransferDeptName(data == null ?"":data.getName());
                SysTenantDTO sysTenantDTO = tenantFeignClient.getTenantByCode(item.getTenantCode()).getData();
                item.setTransferTenantName(sysTenantDTO == null ? null:sysTenantDTO.getTenantName());
            });
        }
        return pageData;
    }
    private QueryWrapper<ArchiveTransferList> getWrapper(Map<String, Object> params){
        String batchNo = (String)params.get("batchNo");
        String transferTitle = (String)params.get("transferTitle");
        String transferState = (String)params.get("transferState");
        QueryWrapper<ArchiveTransferList> wrapper = new QueryWrapper<>();
        wrapper.eq(Constant.DEL_FLAG, DelFlagEnum.NORMAL.value());
        wrapper.eq(StringUtils.isNotBlank(batchNo), "batch_no", batchNo);
        wrapper.eq(StringUtils.isNotBlank(transferState), "transfer_state", transferState);
        wrapper.like(StringUtils.isNotBlank(transferTitle), "transfer_title", transferTitle);
        UserDetail user = SecurityUser.getUser();
        if(user.getSuperAdmin() != SuperAdminEnum.YES.value()) {
            wrapper.eq("tenant_code",user.getTenantCode());
        }
        if(user.getSuperAdmin() != SuperTenantEnum.YES.value()) {
            wrapper.eq("category_id",userFeignClient.getCategoryId());
        }
        return wrapper;
    }

    @Override
    public ArchiveTransferListStatusVo getCountStatus() {
        ArchiveTransferListStatusVo statusVo = new ArchiveTransferListStatusVo();
        List<ArchiveTransferList> archiveTransferLists = this.baseDao.selectList(getWrapper(new HashMap<>(16)));
        if(CollectionUtils.isNotEmpty(archiveTransferLists)) {
            long toReceiveCount = archiveTransferLists.stream().filter(item ->item.getTransferState() == ArchivesConstant.TRANSFER_STATE)
                    .count();

            statusVo.setToReceiveCount(toReceiveCount);
            long alreadyReceivedCount = archiveTransferLists.stream().filter(item -> item.getTransferState() == ArchivesConstant.ALREADY_RECEIVED_STATE)
                    .count();
            statusVo.setAlreadyReceivedCount(alreadyReceivedCount);
            long toAuditCount = archiveTransferLists.stream().filter(item -> item.getTransferState() == ArchivesConstant.TO_AUDIT_STATE)
                    .count();
            statusVo.setToAuditCount(toAuditCount);
        }
        return statusVo;
    }
}
